package com.topscomm.appv2.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.topscomm.appv2.dto.MsgBusDTO;
import com.topscomm.appv2.pojo.AppMessagePushEntity;
import com.topscomm.appv2.pojo.AppMessageSysEntity;
import com.topscomm.appv2.service.IAppMessagePushService;
import com.topscomm.appv2.service.IAppMessageSysService;
import com.topscomm.basic.BasicEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.DatacodeCache;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: yanshunkuan
 * @date: 2023-09-07
 * @descripyion:
 * @modify:
 */
@RestController
@RequestMapping("appv2/messageConsumer")
public class MessageConsumerController {

    public static final String APP_MESSAGE_PUSH = "CboMessageApp";
    public static final String APP_MESSAGE_SYS = "CboMessageSys";

    @Resource
    protected IAppMessagePushService appMessagePushService;
    @Resource
    protected IAppMessageSysService appMessageSysService;

    @PostMapping("/insert")
    public ResponseResult<Object> insert(@RequestBody @Validated MsgBusDTO msgBusDTO) {
        List<JSONObject> dataList = msgBusDTO.getData();
        List<BasicEntity> insertList = new ArrayList<>();
        if (APP_MESSAGE_PUSH.equals(msgBusDTO.getClsName())) {
            for (JSONObject jsonObject : dataList) {
                fillReceiverName(jsonObject);
                AppMessagePushEntity appMessagePushEntity = JSONUtil.toBean(jsonObject, AppMessagePushEntity.class);
                if (!StringUtil.isEmpty(appMessagePushEntity.getContent())) {
                    insertList.add(appMessagePushEntity);
                }
            }
            appMessagePushService.insertBatch(insertList);
        } else if (APP_MESSAGE_SYS.equals(msgBusDTO.getClsName())) {
            for (JSONObject jsonObject : dataList) {
                fillReceiverName(jsonObject);
                insertList.add(JSONUtil.toBean(jsonObject, AppMessageSysEntity.class));
            }
            appMessageSysService.insertBatch(insertList);
        } else {
            return ResponseResult.error("没有匹配到对应的业务对象");
        }
        return ResponseResult.ok(insertList.size());
    }

    private void fillReceiverName(JSONObject jsonObject) {
        if (ConvertUtil.convertToString(jsonObject.get("receivername")).length() == 0 && jsonObject.containsKey("receivercode")) {
            DatacodeCache datacodeCache = (DatacodeCache) SpringUtil.getBean("datacodeCache");
            String usercode = ConvertUtil.convertToString(jsonObject.get("receivercode"));
            String username = datacodeCache.getValueDynamic("CboUser", usercode, "code", "name");
            jsonObject.put("receivername", username);
        }
    }

}
